package com.hqd.ch03.v11.spel.accessor;

import com.hqd.ch03.v11.spel.EvaluationContext;
import com.hqd.ch03.v11.spel.PropertyAccessor;
import com.hqd.ch03.v11.spel.express.BeanExpressionContext;

/**
 * 获取bean对象的属性读取器
 */
public class BeanExpressionContextAccessor implements PropertyAccessor {
    @Override
    public boolean canRead(EvaluationContext context, Object target, String name) {
        return (target instanceof BeanExpressionContext && ((BeanExpressionContext) target).containsObject(name));
    }

    @Override
    public Object read(EvaluationContext context, Object target, String name) {
        return ((BeanExpressionContext) target).getObject(name);
    }

    @Override
    public boolean canWrite(EvaluationContext context, Object target, String name) {
        return false;
    }

    @Override
    public void write(EvaluationContext context, Object target, String name, Object newValue) {
        throw new RuntimeException("Beans in a BeanFactory are read-only");
    }

    @Override
    public Class<?>[] getSpecificTargetClasses() {
        return new Class<?>[]{BeanExpressionContext.class};
    }
}
